package com.kdweibo.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kdweibo4j.WeiboException;



/**
 * @author jason
 * @time 2011-4-11  下午04:45:14
 * 
 */
/**
 * @author jason
 * 
 */
public class Utils {

	public static final String BASE_FILE_DIR = "Kingdee";

	private static Map<String, SimpleDateFormat> formatMap = new HashMap<String, SimpleDateFormat>();

	public static final String DATE_DISPLAY_FORMAT = "yyyy/MM/dd hh:mm:ss";//MM/dd/yyyy HH:mm:ss
	private static final String insertTitle = "#请插入话题名称#";
	// private static final String insertAt = "@请输入用户名";
	private static final String insertAt = "@";

	/*-------自动匹配及自动链接  begin  ---goldenrod.zhu@gmail.com*/
	private static final Pattern NAME_MATCHER = Pattern
			.compile("@[\u4e00-\u9fa5,a-z,A-Z,0-9,_,-]+");
	public static Date parseDate(String str, String format)
			throws WeiboException {
		if (str == null || "".equals(str)) {
			return null;
		}
		SimpleDateFormat sdf = formatMap.get(format);
		if (null == sdf) {
			sdf = new SimpleDateFormat(format, Locale.ENGLISH);
			sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
			formatMap.put(format, sdf);
		}
		try {
			synchronized (sdf) {
				// SimpleDateFormat is not thread safe
				return sdf.parse(str);
			}
		} catch (ParseException pe) {
			throw new WeiboException("Unexpected format(" + str
					+ ") returned from kdweibo.com");
		}
	}
	/**
	 * 
	 * 获取当前时间的相对时间，如：一分钟前，一小时前等
	 * 
	 * @author goldenrod.zhu@gmail.com
	 */
	public static String getRelativeDate(Date date) {
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String currentTime = sdf.format(dt);

		String time = sdf.format(date);
		return currentTime.substring(0, 10).equals(
				time.substring(0, 10)) ? "今天"
				+ time.substring(12, time.length()) : time;
//		Date now = new Date();
//		String min = "分钟";
//		String hour = "小时";
//		String day = "天";
//		String suffix = "前";
//		String nowTime = "刚刚";
//		// Seconds.
//		long diff = (now.getTime() - date.getTime()) / 1000;
//		if (diff < 0) {
//			diff = 0;
//		}
//		// Minutes.
//		diff /= 60;
//		if (diff < 1) {
//			return nowTime;
//		}
//		if (diff < 60) {
//			return diff + min + suffix;
//		}
//		// Hours.
//		diff /= 60;
//		if (diff < 24) {
//			return diff + hour + suffix;
//		}
//		// days.
//		diff /= 24;
//		if (diff < 3) {
//			return diff + day + suffix;
//		}
//		return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
	}


	public static long date2Store(String aDate) throws ParseException {
		long result = 0;
		// try {
		return new SimpleDateFormat("yyyy/MM/dd").parse(aDate).getTime();
		/*
		 * } catch (ParseException e) { Log.e(Client.TAG, "Exception when parse
		 * "+aDate+" in Util.getDate2Store"+aDate,e); } return result;
		 */
	}


	public static boolean isEmptyString(String aStr) {
		return aStr == null || "".equals(aStr) || "".equals(aStr.trim())
				|| "null".equalsIgnoreCase(aStr);
	}

	public static String getStringTypeDate(Date createdAt) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
		String date = dateFormat.format(createdAt)
				.replace("GMT+08:00", "+0800");
		return date;
	}

	public static String date2Display(Long aLongDate) {
		if (isEmptyString(aLongDate.toString()))
			return "";
		// Log.d(Client.TAG, aLongDate);
		/*
		SimpleDateFormat sdf= new SimpleDateFormat(DATE_DISPLAY_FORMAT);
		//前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
		java.util.Date dt = new Date(aLongDate * 1000);  
		String sDateTime = sdf.format(dt);  //得到精确到秒的表示：08/31/2006 21:08:00
        Log.d("sDateTime", "aaa "+sDateTime);
        */
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
      //前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
      java.util.Date dt = new Date(aLongDate * 1000);  
      String sDateTime = sdf.format(dt); 
		return sDateTime;
	}

	

	// 用当前时间给取得的图片命名
	public static String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".jpg";
	}

	// 用当前时间给取得的下载的安装包名
	public static String getDownloadFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyyMMdd_HHmmss");
		return "weibo_"+dateFormat.format(date) + ".apk";
	}
	/**
	 * 删除文件夹
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件夹下的文件
	 */
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}
}
